package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.grade.domain.Grade;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.Stu;
import com.ruoyi.stuinfo.service.IStuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 *
 * @author zc
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/stuinfo/info")
public class StuController extends BaseController
{
    @Autowired
    private IStuService stuService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stu stu)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");

        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<Stu> list = stuService.selectStuList(stu);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId = teacherId.replace("H", "");
            startPage();
            List<Stu> list = stuService.selectStuList_handteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //学生
            startPage();
            List<Stu> list = stuService.selectStuList_student(teacherId);
            return getDataTable(list);
        }


    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stu stu)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");

        if("admin".equals(teacherId))
        {
            //管理员
            List<Stu> list = stuService.selectStuList(stu);
            ExcelUtil<Stu> util = new ExcelUtil<Stu>(Stu.class);
            util.exportExcel(response, list, "学生信息数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<Stu> list = stuService.selectStuList_handteacher(teacherId);
            ExcelUtil<Stu> util = new ExcelUtil<Stu>(Stu.class);
            util.exportExcel(response, list, "学生信息数据");
        }
        else
        {
            //学生
            List<Stu> list = stuService.selectStuList_student(teacherId);
            ExcelUtil<Stu> util = new ExcelUtil<Stu>(Stu.class);
            util.exportExcel(response, list, "学生信息数据");
        }


    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stuService.selectStuById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stu stu)
    {
        return toAjax(stuService.insertStu(stu));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stu stu)
    {
        return toAjax(stuService.updateStu(stu));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:info:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stuService.deleteStuByIds(ids));
    }
}
